package com.byd.android.QRcode;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.byd.android.mediaplayer.BaseActivity;
import com.byd.android.mediaplayer.R;
import com.byd.android.start.MainInterface;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import org.apache.commons.codec.binary.Base64;

import java.lang.reflect.Method;
import java.util.Hashtable;

import static com.byd.android.agreement.InstructionList.ORDER.ORDER_DONOTHING;

public class QrCode extends BaseActivity {
    private static final String TAG = "QrCode";
    private ImageView qrCodeIv;

    private int w;  //图片长度
    private int h;  //图片高度
    private String SSID = "10";
    private String password = "10000001";

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case ORDER_DONOTHING:
                    onBackPressed();
            }

        }
    };

    @Override
    protected void sendHandlerToService() {
        mServiceBinder.sendHandler(handler);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode_image);
        Log.d(TAG, "createQRcodeImage: width is " + w +" height is " + h);
        qrCodeIv = (ImageView) findViewById(R.id.qr_code);
        String url = combineSSIDPwd(SSID,password);
        Log.d("combine", "onCreate: "+url);
        createQRcodeImage(url);


    }
    public static boolean isApOn(Context context) {
        WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        try {
            Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(wifimanager);
        } catch (Throwable ignored) {
        }
        return false;
    }

    public static String encode(byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }

    public String combineSSIDPwd(String ssid,String pwd){
        password = encode(password.getBytes());
        int length = SSID.length();
        String url;
        if(length >= 0&&length <=9){
            url = "10"+length+SSID+password;
        }else if(length >= 10 && length <= 99){
            url = "1"+length+SSID+password;
        }else{
            //用户名太长
            url = "0"+length+SSID+password;
        }
        return  url;
    }

    //转换成二维码QRcode的函数。参数为一个字符串
    public void createQRcodeImage(String url)
    {

//        w=qrCodeIv.getWidth();
//        h=qrCodeIv.getHeight();

        w = 300;
        h = 300;
        Log.d(TAG, "createQRcodeImage: width is " + w +" height is " + h);
        try
        {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1)
            {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, w, h, hints);
            int[] pixels = new int[w * h];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (bitMatrix.get(x, y))
                    {
                        pixels[y * w + x] = 0xff000000;
                    }
                    else
                    {
                        pixels[y * w + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
            //显示到我们的ImageView上面
            qrCodeIv.setImageBitmap(bitmap);
        }
        catch (WriterException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
